import VueRouter from 'vue-router'
import Vue from 'vue'

Vue.use(VueRouter)
const router = new VueRouter({
    mode: 'hash',
    routes: [
        {
            path: '/',
            name: 'home',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../components/BackLogin.vue')
        },
        {
            path: '/backManage',
            name: 'backManage',
            component: () => import('../components/BackManage.vue'),
            redirect: { name: 'settings' },
            children: [
                {
                    path: 'settings',
                    name: 'settings',
                    component: () => import('../components/BackSettings.vue')
                },
                {
                    path: 'userManage',
                    name: 'userManage',
                    component: () => import('../components/UserManage.vue')
                },
                {
                    path: 'bannerList',
                    name: 'bannerList',
                    component: () => import('../components/BannerList.vue')
                },
                {
                    path: 'bannerUpload',
                    name: 'bannerUpload',
                    component: () => import('../components/BannerUpload.vue')
                },
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem('token')
    if (!token) {
        if (to.path === '/login') {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})

export default router